package com.chinatgl.latte.util.app;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import com.chinatgl.latte.app.Latte;


/**
 * 跟App相关的辅助类
 *
 * @author 杨伟乔
 */
public class AppUtils {

    private AppUtils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");

    }

    /**
     * 获取应用程序名称
     */
    public static String getAppName() {
        try {
            PackageManager packageManager = Latte.getApplication() .getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    Latte.getApplication().getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return Latte.getApplication().getResources().getString(labelRes);
        } catch (PackageManager.NameNotFoundException e) {

        }
        return null;
    }

    /**
     * [获取应用程序版本名称信息]
     *
     * @param
     * @return 当前应用的版本名称
     */
    public static String getVersionName() {
        try {
            PackageManager packageManager = Latte.getApplication().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    Latte.getApplication().getPackageName(), 0);

            return packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException e) {

        }
        return null;
    }

    /**
     * [获取应用程序版本名称信息]
     *
     * @param
     * @return 当前应用的版本名称
     */
    public static int getVersionCode() {
        try {
            PackageManager packageManager = Latte.getApplication().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    Latte.getApplication().getPackageName(), 0);

            return packageInfo.versionCode;

        } catch (PackageManager.NameNotFoundException e) {

        }
        return 0;
    }

//    /**
//     * 弹出是否升级的对话框
//     */
//    public  static void showUpdateDialog(final FragmentActivity activity, String  updateInfoLog, final String newVersionCode, final String downLoadUrl) {
//
//        int[] padiing = new int[]{30,0, 30, 30};
//        new SuperDialog.Builder(activity)
//                .setRadius(40)
//                .setAlpha(1.0f)
//                .setWidth(0.7f)
//                .setItemsBottomMargin(0)
//                .setTitle("版本更新")
//                .setMessage(updateInfoLog  , Color.parseColor("#666666"), 50, padiing)
//                .setCanceledOnTouchOutside(true)
//                .setDimEnabled(true)
//                .setNegativeButton("忽略", Color.parseColor("#333333"), new SuperDialog.OnClickNegativeListener() {
//                    @Override
//                    public void onClick(View v) {
//                        //保存下当前的版本已经显示过了更新的信息 下次不会再显示
//                        SpUtils.setString(Contains.IS_UPDATE, String.valueOf(newVersionCode));
//                    }
//                })
//                .setPositiveButton("更新", Color.parseColor("#333333"), new SuperDialog.OnClickPositiveListener() {
//                    @Override
//                    public void onClick(View v) {
//                        downloadApk( activity, downLoadUrl);
//                    }
//                })
//                .build();
//
//    }
//
//    public  static void downloadApk(Context context, String downLoadUrl) {
//        // xUtils
//        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//检查sd卡是否可用
//            Intent intent = new Intent(context, UpdateService.class);
//            intent.putExtra("download_url", downLoadUrl);
//            context.startService(intent);
//
//        } else {
//
//            Toastutils.showToastCenter("SD卡不可用");
//        }
//    }


}
